package com.cy.controller.interceptor;

import com.cy.entity.User;
import com.cy.service.MessageService;
import com.cy.util.HostHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: cy
 * @Date: 2022/02/07/21:31
 * @Description:
 */
@Component
public class MessageIntercepter implements HandlerInterceptor {
    @Resource
    private HostHolder hostHolder;

    @Resource
    private MessageService messageService;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        User user = hostHolder.getUser();
        if (user != null && modelAndView != null){
            Integer lettersCountUnreadCount = messageService.selectLettersCountUnreadCount(user.getId(), null);
            int noticeUnreadCount = messageService.selectNoticeUnreadCount(user.getId(), null);
            modelAndView.addObject("allUnreadCount", lettersCountUnreadCount + noticeUnreadCount);


        }
    }
}
